<script type="text/javascript">
$(function(){
	var pg = App.batch.scheduleIndex.ui, pp = pg.params;
	
	var gd = pg.find('.grid').mac('grid', {
		key: 'id',
		cols: [{
			name: 'no', title : '&nbsp;', width: 18,
			render: function(r, tr, me){
				var a = $('<span class="icon icon-plus"></span>');
				a.click(function(){
					var yy = me.find('.ybar>div'), pl = tr.exPanel;
					if (a.hasClass('icon-plus')) {
						if (pl) {
							pl.show();
						} else {
							pl = $('<div class="expand"></div>');
							pl.append('&nbsp;有效期：' + r.startTime + ' - ' + (r.endTime || '') + '<br>');
							pl.append('&nbsp;定时类：' + r.job + '<br>');
							pl.append('&nbsp;实现类：' + r.impl + '<br>');
							pl.append('&nbsp;备&nbsp;注：' + r.memo);
							tr.exPanel = pl.insertAfter(tr);
							$('<div class="clear"></div>').insertBefore(pl);
						}
						yy.height(yy.height() + pl.height());
						a.removeClass('icon-plus').addClass('icon-minus');
					} else {
						pl.hide();
						yy.height(yy.height() - pl.height());
						a.removeClass('icon-minus').addClass('icon-plus');
					}
				});
				return a;
			}
		},{
			field: 'catalog', title : '所属系统', width: 120, 
			render: function(r, tr, me){
				return $('<span class="a"></span>').click(function(){
					catalogLb.text(r.catalog);
					me.load({ catalog: r.catalog, pageNo: 1, key: '' })
				}).append(r.catalog);
			}
		},{
			field: 'code', title : '编号', width: 160,
			render: function(r, tr, me){
				return $('<span class="a"></span>').click(function(){
					var p = pg.parent();
					if (App.logon.power('batch.info.save')){
						App.show({
							title: '批量信息',
							ctx: 'batch',
							action: 'infoEdit',
							params: r
						}, function(r, s){
							App.show({
								ctx : 'batch',
								action : 'infoIndex',
								params : {}
							}, $.noop, p);
						}, p);
					}
				}).append(r.code);
			}
		}, {
			field: 'cron', title : '表达式', width: 120
// 		},{
// 			field: 'startTime', title : '有效期从', width: 240
// 		},{
// 			field: 'endTime', title : '至', width: 240
// 		},{
// 			field: 'job', title : 'JOB类', width: 240
		},{
			field: 'nextFireTime', title : '下一次触发时间', width: 168
		},{
			field: 'previousFireTime', title : '上一次触发时间', width: 168
		},{
			field: 'id', title : '操作', width: 40,
			render: function(r, tr, gd) {
				return $('<span class="a">删除</span>').click(function(){
					mac.confirm('确定要 删除 此定时计划吗？', function(){
						$.post(App.ctx + '/schedule/delete.do', {
							id: r.id,
							impl: r.impl
						}, function(rs){
							gd.load();
						});
					});
				});
			}
		}],
		loader: {
			url: App.ctx + '/schedule/list.do',
			params: {
				pageNo: 1, pageSize: 10
			},
			autoLoad: true
		},
		pagerLength: 10
	});
	pg.seek('key').keydown(function(e) {
		if (e.keyCode == 13) {
			gd.load({ key : $(this).val() });
		}
	});
	pg.seek('back').click(function(){
		catalogLb.text('全部批量');
		gd.load({ catalog: '', pageNo: 1, key: '' });
	});
	
	var w = mac.wait('Loading');
	$.get(App.ctx + '/schedule/status.do', {
		//
	}, function(rs) {
		w.close();
		var ro = mac.eval(rs);
		if (ro.success) {
			var aa = ['start', 'stop'], act = aa[ro.data * 1];
			if (App.logon.power('batch.schedule.' + act)){
				pg.seek(act).click(function(){
					mac.confirm('确定要 ' + $(this).text() + ' 吗？', function(){
						$.post(App.ctx + '/schedule/' + act + '.do', {
							//
						}, function(){
							var ro = mac.eval(rs);
							if (ro.success) {
								if (act == 'start') {
									mac.alert('启动定时器成功，所有计划将恢复运行');
								} else {
									mac.alert('停止定时器成功，所有计划将暂停运行');
								}
							} else {
								mac.alert(ro.data, ro.params);
							}
						});
					});
				}).show();
			}
		} else {
			mac.alert(ro.data, ro.params);
		}
	}); 

	function adjust(){
		var p = pg.parent(), w = p.width(), h = p.height();
		pg.width(w - 64);
		gd.parent().height(h - 96);
		gd.adjust();
	}
	$(window).resize(adjust);
	adjust();
});
</script>
<div class="navi">
	<span class="a" name="back">定时计划</span>&gt;&gt;
	<span name="title">全部计划</span>
</div>
<div class="head">
	<input type="text" name="key" class="fr" placeholder="查找" />
	<button name="start" class="hidden">启动定时器</button>
	<button name="stop" class="hidden">停止定时器</button>
</div>
<div class="body">
	<div class="grid"></div>
</div>